//
//  RecordViewController.m
//  LoveDraw
//
//  Created by 梁晨 on 2017/6/13.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "RecordViewController.h"
#import "MySignTableViewCell.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"
#import "RecordModel.h"

@interface RecordViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
}

@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;

@end

@implementation RecordViewController

-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(UITableView *)listV
{
    if (!_listV) {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.separatorStyle = 0;
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        _listV.backgroundColor = [UIColor whiteColor];
        [_listV registerClass:[MySignTableViewCell class] forCellReuseIdentifier:@"mysigncell"];

    }
    return _listV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listV];
     page = 1;


   self.listV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [self doRefreshSenior];
   }];

    self.listV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self Request];
    }];

    [self.listV.mj_header beginRefreshing];
}

-(void)doRefreshSenior
{
    page = 1;
    [self Request];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.listArr.count == 0) {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nodata"]];
    }else
    {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return self.listArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecordModel *model = self.listArr[indexPath.row];
    if ([model.isverify isEqualToString:@"2"])
    {
        return;
    }

    UIAlertController *alertV = [UIAlertController alertControllerWithTitle:@"消息提示框" message:@"是否确认点名" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    UIAlertAction *cimmit = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action)
    {
        [RequestManager HudRequestWithUrl:@"/index.php?g=App&m=StudentCoursetable&a=confirmSign" parmDic:@{@"lid":model.lid} requestType:RequestGET view:self.view Success:^(id data)
        {
            model.isverify = @"2";
            [self.listV reloadData];
        } error:^(NSError *error) {

        }];
    }];
    [alertV addAction:cancel];
    [alertV addAction:cimmit];
    [self presentViewController:alertV animated:YES completion:nil];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecordModel *model = self.listArr[indexPath.row];
    MySignTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mysigncell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.leftYear setText:model.date_year];
    [cell.leftMonth setText:model.date_md];
    [cell.leftImage setImage:[UIImage imageNamed:@"round"]];
    [cell.titleLabel setText:model.coursename];
    [cell.remarkLabel setText:model.Minedescription];

    [UILabel changeLineSpaceForLabel:cell.remarkLabel WithSpace:5.0];

    if([model.isverify isEqualToString:@"1"]){
        [cell.statusLabel setTextColor:[UIColor redColor]];
        [cell.statusLabel setText:@"待确认"];
    }else{
        [cell.statusLabel setTextColor:[UIColor blackColor]];
        [cell.statusLabel setText:@"已确认"];
    }

    if (indexPath.row  == 0) {
        [cell.lineView setHidden:YES];
    }else
    {
        [cell.lineView setHidden:NO];
    }

    if (indexPath.row  == self.listArr.count-1) {
        [cell.bottomLineV setHidden:YES];
    }else
    {
        [cell.bottomLineV setHidden:NO];
    }

    return cell;
}

//124678
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"点名记录";
}

-(void)Request
{
    if (self.aid.length == 0) {
        self.aid = @"";
    }else
    {

    }
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCoursetable&a=signLogs" parmDic:@{@"aid":self.aid,@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
    {
        
        NSArray *Arr = [RecordModel modelConfigure:data];
        if (page == 1) {
            [self.listArr removeAllObjects];
        }
        if (Arr.count==0) {
            page--;
        }

        [self.listArr addObjectsFromArray:Arr];

        [self.listV reloadData];

        [self.listV.mj_footer endRefreshing];

        [self.listV.mj_header endRefreshing];

        if (Arr.count == 0||Arr.count<10)
        {
            self.listV.mj_footer.state = MJRefreshStateNoMoreData;
        }
    } error:^(NSError *error) {
         [self.listV.mj_header endRefreshing];
        [self.listV.mj_footer endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
